package com.kuang.config;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author FHL
 */
public class LoginHandleInterceptor implements HandlerInterceptor {

    /**登录拦截器（true：放行，false：拦截）
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //登录成功就会有用户的session（可以拿这个session来判断用户是是否登录成功了）
        Object loginUser = request.getSession().getAttribute("loginUser");
        if (loginUser==null){
            //没有登录成功
            request.setAttribute("msg","您没有权限，请先登录！");
            request.getRequestDispatcher("/index.html").forward(request,response);
            return false;
        }else {
            return true;
        }
    }
}
